<?php
	include "conexion_bd.php";
	$mysqli->set_charset("utf8");
	$r = $mysqli->query("SELECT * FROM ".$_POST["tabla"]." WHERE ".$_POST["campo"]."='".$_POST["dato"]."';");
	$fila = $r->fetch_assoc();

	$r2 = $mysqli->query("SELECT * FROM auxiliar_docente WHERE ci='".$fila["ci"]."';");
	$tipo = "Auxiliar Docente";
	if(!mysqli_num_rows($r2))
	{
		$r2 = $mysqli->query("SELECT * FROM estudiante WHERE ci='".$fila["ci"]."';");
		$tipo = "Estudiante";
		if(!mysqli_num_rows($r2))
		{
			$r2 = $mysqli->query("SELECT * FROM personal_administrativo WHERE ci='".$fila["ci"]."';");
			$tipo = "Personal Administrativo";
			if(!mysqli_num_rows($r2))
			{
				$r2 = $mysqli->query("SELECT * FROM preparador WHERE ci='".$fila["ci"]."';");
				$tipo = "Preparador";
				if(!mysqli_num_rows($r2))
				{
					$r2 = $mysqli->query("SELECT * FROM profesor WHERE ci='".$fila["ci"]."';");
					$tipo = "Profesor";
				}
			}
		}
	}


?>

<div class="col-lg-6">
	<div class="panel panel-default">
		<div class="panel-body" style = "background: #CEE3F6">
		
		<div class="form-group col-xs-6"  style="margin-left:50%">
			</br><label style="color: rgb(224, 73, 1);"><span class="glyphicon glyphicon-asterisk" id="icon"/>&nbsp;Campos obligatorios</label>
		</div>
		
			<div class="col-lg-12"><h2>Modificar<b id="resaltador"> &nbsp; Personal</b></h2><br/><br/></div>
			<div class="col-xs-12">
				<form id = "myForm" role="form" enctype="multipart/form-data" action="insertar_registro.php" method="post">
					<div class="form-group col-xs-4" id = "cedula">
						<label><span class="glyphicon glyphicon-asterisk" id="icon"/>Cédula&nbsp;</label>
						<input type="text" class="form-control" name="cedula" required>
					</div>
					<div class="col-xs-12"></div>
					<div class="form-group col-xs-6">
						<label><span class="glyphicon glyphicon-asterisk" id="icon"/>Primer Nombre&nbsp;</label>
						<input type="text" class="form-control" name="primer_nombre" required>
					</div>
					<div class="form-group col-xs-6">
						<label>Segundo Nombre</label>
						<input type="text" class="form-control" name="segundo_nombre">
					</div>
					<div class="form-group col-xs-6">
						<label><span class="glyphicon glyphicon-asterisk" id="icon"/>Primer Apellido&nbsp;</label>
						<input type="text" class="form-control" name="primer_apellido" required>
					</div>
					<div class="form-group col-xs-6">
						<label>Segundo Apellido</label>
						<input type="text" class="form-control" name="segundo_apellido">
					</div>
					<div class="form-group col-xs-8">
			          	<label><span class="glyphicon glyphicon-asterisk" id="icon"/>Sexo&nbsp;</label>
			          	<div>
							<input type="radio" name="sexo" value="M" checked>&nbsp;Masculino &nbsp;&nbsp;&nbsp;
							<input type="radio" name="sexo" value="F">&nbsp;Femenino
						</div>
					</div>
					<div class="col-xs-12"></div>
					<div class="form-group col-xs-6">
						<label for="correo_inst"><span class="glyphicon glyphicon-asterisk" id="icon"/>Correo Institucional&nbsp;</label>
						<input type="email" class="form-control" name="correo_inst" type = "email" required>
					</div>
					<div class="form-group col-xs-6">
						<label for="correo_alt">Correo Alternativo</label>
						<input type="email" class="form-control" name="correo_alt" type = "email">
					</div>
					<div class="form-group col-xs-6">
						<label for="main_phone"><span class="glyphicon glyphicon-asterisk" id="icon"/>Teléfono Principal&nbsp;</label>
						<input type="text" class="form-control" name="main_phone" required>
					</div>
					<div class="form-group col-xs-6">
						<label for="alt_phone"><span class="glyphicon glyphicon-asterisk" id="icon"/>Teléfono Alternativo&nbsp;</label>
						<input type="text" class="form-control" name="alt_phone" required>
					</div>
					<div class="form-group col-xs-12">
						<label><span class="glyphicon glyphicon-asterisk" id="icon"/>Dirección&nbsp;</label>
						<textarea class="form-control" rows="3" name="address"></textarea>
					</div>
					<div class="form-group col-xs-12">
						<label><span class="glyphicon glyphicon-asterisk" id="icon"/>Tipo Personal</label>
						<select class="form-control" name ="tipoPersona" disabled>
							<option></option>
							<option>Auxiliar Docente</option>
							<option>Estudiante</option>
							<option>Personal Administrativo</option>
							<option>Preparador</option>
							<option>Profesor</option>
						</select>
					</div>
					<div class="col-xs-12"></br></div>
					<div class="col-xs-4"></div>
					<div class="col-xs-4">	
						<button type="submit" id="agregar" class="btn btn-primary form-control">Actualizar</button>
					</div>
					<div class="col-xs-4"></div>
					<div class="col-xs-12"></br></div>
					<input type="hidden" name="tabla" value="persona">
				</form>
			</div> 
		</div>
	</div>
</div>

<div class="col-lg-6">
	<div class="panel panel-default">
		<div id = "formulario_continuacion" class="panel-body" style = "background: #CEE3F6">
		</div>
	</div>
</div>

<script type="text/javascript">

$('[name=cedula]').val("<?php echo $fila['ci'];?>");
$('[name=primer_nombre]').val("<?php echo $fila['primer_nombre'];?>");
$('[name=segundo_nombre]').val("<?php echo $fila['segundo_nombre'];?>");
$('[name=primer_apellido]').val("<?php echo $fila['primer_apellido'];?>");
$('[name=segundo_apellido]').val("<?php echo $fila['segundo_apellido'];?>");
$('[name=correo_inst]').val("<?php echo $fila['email_institucional'];?>");
$('[name=correo_alt]').val("<?php echo $fila['email_alternativo'];?>");
$('[name=main_phone]').val("<?php echo $fila['telefono_1'];?>");
$('[name=alt_phone]').val("<?php echo $fila['telefono_2'];?>");
$('[name=address]').val("<?php echo $fila['direccion'];?>");
if("<?php echo $fila['sexo'];?>" == "M") $('[name=sexo][value=M]').attr("checked","checked");
else $('[name=sexo][value=F]').attr("checked","checked");


$('[name=tipoPersona]').find('option').filter(function() {return $(this).html() === "<?php echo $tipo;?>";}).attr("selected","selected");

var tipo = "<?php echo $tipo;?>";
if(tipo == "Auxiliar Docente") $.ajax({type:"POST",url:"agregar_auxiliar.html",success:function(result){$("#formulario_continuacion").html(result);}});
if(tipo == "Estudiante") $.ajax({type:"POST",url:"agregar_estudiante.html",success:function(result){$("#formulario_continuacion").html(result);}});
if(tipo == "Personal Administrativo") $.ajax({type:"POST",url:"agregar_personal.html",success:function(result){$("#formulario_continuacion").html(result);}});
if(tipo == "Preparador") $.ajax({type:"POST",url:"agregar_preparador.html",success:function(result){$("#formulario_continuacion").html(result);}});
if(tipo == "Profesor") $.ajax({type:"POST",url:"agregar_profesor.html",success:function(result){$("#formulario_continuacion").html(result);}});
if(tipo == "") $("#formulario_continuacion").html('');
$.ajax({type:"POST",url:"cargar_centros.php",success:function(result){$("[name=centro]").html(result);}});


$('[name=cedula]').keyup(function (e) {
	var code = $(this).val(); 
	if(/^[0-9]+$/.test(code)){
		$.post('validaciones.php', {'cedula':code}, function(data) {
			if(data=='SI'){$("#cedula").addClass("has-error");}
			else $("#cedula").removeClass("has-error");
		});
	}
	else $("#cedula").addClass("has-error");
});
</script>
